package com.our.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.our.biz.NewsBiz;
import com.our.biz.NewsTypeBiz;
import com.our.biz.RepResourceBiz;
import com.our.model.News;
import com.our.model.RepResource;
import com.our.model.Users;
import com.our.util.BusinessException;
import com.our.util.Constants;
@Controller
@RequestMapping("/news")
public class NewsController {
	@Resource
	private  NewsBiz biz;
	@Resource
	private NewsTypeBiz ntbiz;
	@Resource
	private RepResourceBiz resbiz;
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
	}
	
	@RequestMapping("/details/{newsId}")
	public String details(@PathVariable int newsId, Map<String, Object> request){
		request.put("news", biz.findById(newsId));
		return "/news/details";
	}
	
	@RequestMapping("/list")
	public String list(Map<String, Object> request){
		request.put("list", biz.findAll());
		return "/news/list";
	}
	@RequestMapping("/listforUser")
	public String listforUser(int typeId ,Map<String, Object> request){
		request.put("list", biz.findByTypeId(typeId));
		return "/news/list_user";
	}
	@RequestMapping("/preAdd")
	public String preAdd(Map<String, Object> request){
		request.put("list", ntbiz.findAll());
		return "/news/add";
	}
	@RequestMapping("/preUpdate/{newsId}")
	public String preUpdate(@PathVariable int newsId, Map<String, Object> request){
		request.put("list", ntbiz.findAll());
		request.put("news", biz.findById(newsId));
		return "/news/update";
	}
	@RequestMapping("/delete/{newsId}")
	public String delete(@PathVariable int newsId){
		biz.delete(newsId);
		return "redirect:/news/list";
	}
	@RequestMapping("/add")
	public String add(News news,MultipartHttpServletRequest request){
		MultipartFile file = request.getFile("file");
		String fileName = UUID.randomUUID().toString();
		File newfile = new File(Constants.FILE_PATH + file.getOriginalFilename());
		try {
			file.transferTo(newfile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		RepResource res = new RepResource();
		res.setResId(fileName);
		res.setResName(file.getOriginalFilename());
		res.setResPath(file.getOriginalFilename());
		res.setResType(file.getContentType());
		news.setRes(res);
		try {
			resbiz.save(res);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		biz.save(news);
		return "redirect:/news/list";
	}
	@RequestMapping("/update")
	public String update(News news,MultipartHttpServletRequest request,HttpSession session){
		Users user = (Users) session.getAttribute("currentUser");
		MultipartFile file = request.getFile("file");
		if(file.getSize() !=0){
			String fileName = UUID.randomUUID().toString();
			File newfile = new File(request.getSession().getServletContext().getRealPath("") +request.getSession().getServletContext().getRealPath("") +Constants.FILE_PATH  + file.getOriginalFilename());
			try {
				file.transferTo(newfile);
			}catch (Exception e) {
				e.printStackTrace();	
			}
			RepResource res = new RepResource();
			res.setResId(fileName);
			res.setResName(file.getOriginalFilename());
			res.setResPath(file.getOriginalFilename());
			res.setResType(file.getContentType());
			news.setRes(res);
		}
		news.setUpdator(user.getUsername());
		biz.update(news);
		return "redirect:/news/list";
	}
	
}
